{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a6761982-f67e-4a62-aae3-6e19ab11d4d2",
   "metadata": {},
   "source": [
    "## 2410000-XXX-实验2-2025-09-16"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfd9dc66-4a62-4562-b490-24a8a8608e39",
   "metadata": {},
   "source": [
    "1.任务1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "96662a8d-1706-4852-8f1d-27be7255bafa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入十进制整数： 18\n",
      "请输入转换进制(2/8/16)： 2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "十进制数: 18=(1 0 0 1 0) 2进制\n"
     ]
    }
   ],
   "source": [
    "class NumberConverter:\n",
    "    def __init__(self):\n",
    "        self.num = None\n",
    "        self.base = None\n",
    "        self.result = None\n",
    "    \n",
    "    def get_input(self):\n",
    "        \"\"\"获取用户输入\"\"\"\n",
    "        self.num = int(input(\"请输入十进制整数：\"))\n",
    "        \n",
    "        while True:\n",
    "            try:\n",
    "                self.base = int(input(\"请输入转换进制(2/8/16)：\"))\n",
    "                if self.base not in [2, 8, 16]:\n",
    "                    raise ValueError(\"只支持转换为2进制、8进制或16进制\")\n",
    "                break\n",
    "            except ValueError as e:\n",
    "                print(f\"输入错误！{e}\")\n",
    "    \n",
    "    def convert(self):\n",
    "        \"\"\"执行进制转换\"\"\"\n",
    "        if self.num is None or self.base is None:\n",
    "            raise ValueError(\"请先调用get_input方法获取输入\")\n",
    "            \n",
    "        result = []\n",
    "        div = self.num\n",
    "        \n",
    "        # 处理0的情况\n",
    "        if div == 0:\n",
    "            self.result = \"0\"\n",
    "            return\n",
    "            \n",
    "        while div != 0:\n",
    "            div, mod = divmod(div, self.base)\n",
    "            \n",
    "            # 处理16进制的情况（A-F）\n",
    "            if self.base == 16 and mod >= 10:\n",
    "                # 将10-15转换为A-F\n",
    "                mod_char = chr(ord('A') + mod - 10)\n",
    "                result.append(mod_char)\n",
    "            else:\n",
    "                result.append(str(mod))\n",
    "                \n",
    "        result.reverse()\n",
    "        self.result = ' '.join(result)\n",
    "    \n",
    "    def display(self):\n",
    "        \"\"\"显示转换结果\"\"\"\n",
    "        if self.result is None:\n",
    "            self.convert()\n",
    "            \n",
    "        print(f'十进制数: {self.num}=({self.result}) {self.base}进制')\n",
    "\n",
    "\n",
    "def main():\n",
    "    converter = NumberConverter()\n",
    "    converter.get_input()\n",
    "    converter.display()\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6821781-ef29-4c06-9789-a53753c8031a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
